/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cratos.xxgram.platf.es;

import com.cratos.xxgram.platf.base.EsEntity;
import com.cratos.xxgram.platf.base.BaseService;
import com.cratos.xxgram.platf.message.MessageRecord;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.redkale.boot.Application;
import org.redkale.service.RetResult;
import org.redkale.util.Comment;
import org.redkale.util.Utility;
import java.util.Map;
import java.util.HashMap;
import javax.annotation.Resource;
import org.redkale.convert.json.JsonConvert;
/**
 *
 * @author SquRab
 * @param <T>
 */
public class ElasticSearchService<T extends  EsEntity> extends BaseService {
    
    @Resource
    private JsonConvert jsonConvert = JsonConvert.root();
            
    private String url = "http://localhost:9200/";
    
    Map<String, String> header = new HashMap();
    {
        header.put("Content-Type", "application/json");
    }
    
    @Comment("保存数据")
    public RetResult save(String id, T data) {
        String uri = url + data.getIndexname() + "/" + data.getTypename() + "/" + id;
        try {
            String response = Utility.postHttpContent(uri, header, jsonConvert.convertTo(data));
        } catch (IOException ex) {
            Logger.getLogger(ElasticSearchService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return RetResult.success();
    }
    
//    public 
    public static void main(String[] args) throws Exception {
         ElasticSearchService service = Application.singleton(ElasticSearchService.class);
         MessageRecord record = new MessageRecord();
         record.setIndexname("record");
         record.setTypename("xxx-111");
         record.setContent("xxxxx");
         service.save("1",new MessageRecord());
    }
}
